package ui.swing;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import spring.SpringApplicationCtx;
import ui.swing.model.StockInfoModel;

import com.stock.StockQuery;

public class QueryUi extends JDialog {

	private static final long serialVersionUID = 2999447645250092899L;
	private final JPanel contentPanel = new JPanel();
	private JTextField textField;
	private JScrollPane scrollPane = new JScrollPane();
	private JTable table = new JTable();
	private String result = "";
	private StockQuery stockQuery = (StockQuery) SpringApplicationCtx.getCtx().getBean("StockQuery");

	public String getResult() {
		return result;
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			QueryUi dialog = new QueryUi();
			System.out.println(dialog.getResult());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public QueryUi() {
		
		setBounds(100, 100, 597, 409);
		getContentPane().setLayout(new BorderLayout());
		FlowLayout fl_contentPanel = new FlowLayout();
		contentPanel.setLayout(fl_contentPanel);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.NORTH);
		{
			JLabel label = new JLabel("请输入查询码：");
			contentPanel.add(label);
		}
		{
			textField = new JTextField();
			contentPanel.add(textField);
			textField.setColumns(10);
		}
		{
			JButton button = new JButton("查询");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String value = textField.getText();
					if(value.equals("")){
						return;
					}
					String sql = "FROM Stockinfo s WHERE s.number like '%"+value+"%' or s.name like '%"+value+"%' or s.shortname like '%"+value+"%' ";
					List list = stockQuery.getAll(sql);
					StockInfoModel stm = new StockInfoModel(list);
					
					table.setModel(stm);
					scrollPane.setViewportView(table);
					if(list.size()==1){
						result = stm.getValueAt(0, 0).toString();
						dispose();
					}
				}
			});
			contentPanel.add(button);
			getRootPane().setDefaultButton(button);
		}
		
		table.setColumnSelectionAllowed(false);
		table.setRowSelectionAllowed(true);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount()>1){
					result = table.getValueAt(table.getSelectedRow(), 0).toString();
					dispose();
				}else{
					System.out.println("aa："+e.getClickCount());
				}
			}
		});
		
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("确定");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						result = table.getValueAt(table.getSelectedRow(), 0).toString();
						dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
			}
			{
				JButton cancelButton = new JButton("取消");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		{
			
			getContentPane().add(scrollPane, BorderLayout.CENTER);
		}
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setModal(true);
		setVisible(true);
	}

}
